import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Req,
} from '@nestjs/common';
import { TagService } from './tag.service';
import { CreateTagDto } from './dto/create-tag.dto';
import { UpdateTagDto } from './dto/update-tag.dto';
import { AuthGuard } from '@nestjs/passport';
import { ToolsService } from 'src/utils/tools.service';
import { TagEntity } from './entities/tag.entity';
import { ApiTags } from '@nestjs/swagger';


@ApiTags('书签模块')  
@Controller('tag')
export class TagController {
  constructor(private readonly tagService: TagService) {}

  @Post('/add')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  create(@Body() createTagDto: CreateTagDto, @Req() req) {
    try {
      return this.tagService.create(createTagDto, req.user.userid);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Post('/addUseSql')
  createUseNativeSql(@Body() createTagDto: CreateTagDto) {
    try {
      return this.tagService.createUseNativeSql(createTagDto);
    } catch (error) {
      ToolsService.fail(error);
    }
  }


  @Get('/list')
  findAll(
    @Query('pageSize') pageSize: number,
    @Query('currentPage') currentPage: number,
    @Query('tagname') tagname: string,
    @Query('userId') userId: string,
  ) {
    return this.tagService.findAll(
      tagname,
      {
        pageSize: pageSize ? pageSize : 100,
        currentPage: currentPage ? currentPage - 1 : 0,
      },
      userId,
    );
  }

  @Get(':id')
 async findOne(@Param('id') id: number) {
    let  tag = await  this.tagService.findOne(id);
    return tag;
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateTagDto: UpdateTagDto) {
    try {
      return this.tagService.update(id, updateTagDto);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    try {
      return this.tagService.remove(id);
    } catch (error) {
      ToolsService.fail(error);
    }
  }
}
